<?php
namespace app\restaurantadmin\controller;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
        $uid=session('id');
        $shop= \think\Db::name('dining') ->where('admin_id',$uid)->find();
        $this->assign('shop',$shop);
        // var_dump($shop);die();
        $shopid=$shop['shop_id'];
        // echo $shopid;die();

        //餐厅评分
        $shopscore= \think\Db::name('score') ->where('shop_id',$shopid)->find();
        $this->assign('shopscore',$shopscore);


        //店铺公告
        $noticelist= \think\Db::name('notice')->where('did',$shopid)->group('id desc')->limit(6)->select();
        $this->assign('noticelist',$noticelist);

        return $this->fetch();
    }

    public function addshop()
    {
        $uid=session('id');
        $shoptime=input('shoptime_m').'am-'.input('shoptime_a').'pm';

        if(request()->isPost()){
            $data=[
                'shopname'=>input('shopname'),
                'address'=>input('address'),
                'phonenum'=>input('phonenum'),
                'service'=>input('service'),
                'shoptime'=>$shoptime,
                'state'=>'0',
                'intro'=>input('intro'),
                'comment'=>input('comment'),
                'admin_id'=>$uid,
                'createtime'=>date("Y-m-d H:i:s"),

            ];


            //上传图片
            if($_FILES['pic']['tmp_name']){

                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('pic');
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/index/Uploads/shoppic');
                if($info){

                    $data['pic']=date('Ymd').'/'.$info->getFilename();
                    // echo $data['pic'];
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    // echo $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            // dump($data);    die();

            //添加数据前应该先验证
            $validate = \think\Loader::validate('Addshopval');

            if($validate->check($data)){
                $db=\think\Db::name('dining')->insert($data);
                if($db){
                    return $this->redirect('Index/validate_shop');
                }
                else{
                    return $this->error('添加失败');
                }
            }else{
                $this->error($validate->getError());
            }
            return;



        }
        return $this->fetch();
    }

    public function validate_shop()
    {
        return $this->fetch();
    }

    //暂停营业/正常营业
    public function changediningstate(){
        $shop_id=input('shop_id');//获取id
        //$state=input('state');//获取state
        //echo $state;die;
        if($db=\think\Db::name('dining')->where('shop_id',$shop_id)->update(['state' =>input('state')])){
            return $this->redirect('Index/index');
        }else{
            return $this->error('餐厅状态状态重置失败！','dininglist');
        }
        return;
    }

    //发布公告信息
    public function addnotice(){
        $uid=session('id');
        // echo("<script type='text/javascript'> alert($uid);</script>");
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;

        if(request()->isPost()){
            $data=[
                'notice'=>input('notice'),
                'did'=>$shopid,
                'ndate'=>date('Y-m-d H:m:s'),
            ];
            // var_dump($data) ;die;
            //添加数据前应该先验证
            $validate = \think\Loader::validate('Noticeval');

            if($validate->check($data)){
                $db=\think\Db::name('notice')->insert($data);
                if($db){
                    return $this->redirect('Index/index');
                }
                else{
                    echo("<script type='text/javascript'> alert('添加失败！');</script>");
                }
            }else{
                // echo $validate->getError();
                $this->error($validate->getError());
                // $error=$validate->getError();
                // echo("<script type='text/javascript'> alert($error);</script>");
            }

        }
    }


    //显示修改界面
    public function edit_shop(){
        $uid=session('id');
        $shop= \think\Db::name('dining') ->where('admin_id',$uid)->find();
        $shopapm=$shop['shoptime'];
        $shop_m=substr($shopapm, 0, -10);
        $shop_a=substr($shopapm, -7, -2);
        $this->assign('shop',$shop);
        $this->assign('shop_m',$shop_m);
        $this->assign('shop_a',$shop_a);
        return $this->fetch();
    }

    //修改店铺信息
    public function editshop(){
        $uid=session('id');
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;
        $shoptime=input('shoptime_m').'am-'.input('shoptime_a').'pm';



        //获取修改后的数据
        if(request()->isPost()){
            $data=[

                'shopname'=>input('shopname'),
                'address'=>input('address'),
                'phonenum'=>input('phonenum'),
                'service'=>input('service'),
                'shoptime'=>$shoptime,
                'intro'=>input('intro'),
                'comment'=>input('comment'),
                'admin_id'=>$uid,

            ];




            //上传图片
            if($_FILES['pic']['tmp_name']){


                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('pic');

                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/admin/Uploads/shoppic');
                if($info){


                    $data['pic']=date('Ymd').'/'.$info->getFilename();
                    // echo $data['pic'];die;
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    // echo $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }

            // $type_id=input('type_id');
            if($db=\think\Db::name('dining')->where('shop_id',$shopid)->update($data)){

                // echo $type_id;
                // die();
                // dump($data['type_id']);die();
                // header("location:'javascript:history.back(-1);");
                // return $this->redirect('Food/food/',array('type_id'=>$type_id));
                return $this->redirect('Index/index');

            }else{
                return $this->error('修改失败！');
            }
            return;

        }

        return $this->fetch();

    }






}

?>